<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Classifieds extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		//check session
		 $this->session->set_userdata(array('redirectUrl' =>current_url()));
		 check_user_login();
		
	}

	/**
	 load index file
	**/
	function index() {
		$this->_display((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
	}
	//get all the classifieds ads
	function view()
	{
		$crud = new grocery_CRUD();
		$crud->where('advertiserID',(int) $this->session->userdata('userid'));
		$crud->order_by('posted_date', 'desc');
		$crud->set_table('classified');

		$crud->display_as('categoryID', 'Category');
		$crud->display_as('advertiserID', 'Advertiser Name');
		$crud->display_as('type', 'You want to');

		$crud->set_relation('categoryID','adv_category','cat_descriptions');
		$crud->set_relation('advertiserID','masteruser','firstName');
		
		$crud->set_subject('Classifieds');
		$crud->columns('title','description','categoryID','posted_date','expired_date');

		$crud->field_type('price_denote','dropdown',
            array('KS' => 'KS', 'LAKH'=> 'LAKH','USD' => 'USD' , 'SGD' => 'SGD', 'CNY' => 'CNY'));
		$crud->field_type('type','dropdown',
            array('1' => 'Sell', '2'=> 'Buy','3' => 'Trade' , '4' => 'Rent'));
		$crud->field_type('isSpecialDiscount','dropdown',
            array(0 => 'No', 1 => 'Yes'));

		/**
		$crud_columns('title','description','categoryID','posted_date') to set the number of columns display on list page
		$crud_fields('','','','') to set the number of columns display on details page
		$crud->display_as('lastName','Last Name'); to set display name
		**/
		$crud->unset_columns('Advertiser Name','contact_person','contact_email','contact_number','primary_image','small_image1','small_image2','small_image3');
		$crud->edit_fields('type','title','description','categoryID','contact_person','contact_number','contact_email','price','price_denote','isSpecialDiscount' ,'percentage','quantity','primary_image','small_image1','small_image2','small_image3');
		
		$crud->set_field_upload('primary_image','assets/uploads/classifieds');
		$crud->set_field_upload('small_image1','assets/uploads/classifieds');
		$crud->set_field_upload('small_image2','assets/uploads/classifieds');
		$crud->set_field_upload('small_image3','assets/uploads/classifieds');
		$crud->unset_add();
		$output = $crud->render();
		
		$crud->unset_jquery_ui();
		$this->_display($output);
	}
	//private function output
	function _display($output = null)
	{
		
		$this->load->view('admin/classifieds/index.php',$output );	
	}

	// post new general advertisement (any categories)
	function post(){

		$data['title'] = 'Post Classifieds ad';
		$data['message'] = '';
		$this->load->model('User_model','',TRUE);
		$userID = (int) $this->session->userdata('userid');
		$data['user'] = $this->User_model->get_by_id($userID);
		$current_date = date('Y-m-d H:m:s');
		$data['expired_date'] = calculate_expiry_date($current_date, '+14 day');
		$this->load->model('Classifiedscatg_model','',TRUE);
		$data['catg'] = $this->Classifiedscatg_model->get_classifieds_categories();
		$data['action'] = site_url('admin/classifieds/add');
		$this->load->view('admin/classifieds/form', $data);

	}
	// post add
	function add(){
		 // set common properties
		$data['title'] = 'Post Classifieds ad';
		// run form_validation
		if ($this->form_validation->run('classifiedpostfrm') == FALSE){
			$this->post();
		}else{
			// save data
			// UI to add price as default 0.0
			$this->load->model('Classifiedscatg_model','',TRUE);
			$adv = array('title' => $this->input->post('title'),
					'description' =>$this->input->post('description'),
					'price' => $this->input->post('pprice'),
					'categoryID' => $this->input->post('selectCatg'),
					'contact_number' => $this->input->post('contact_no'),
					'contact_email' => $this->input->post('contact_email'),
					'contact_person'=> $this->input->post('contact_person'),
					'type' => $this->input->post('type')
					);
			if ($this->input->post('quantity'))
				$adv['quantity'] = $this->input->post('quantity');
			
			if($this->input->post('percentage'))
				$adv['percentage'] = $this->input->post('percentage');
			
			if($this->input->post('discounted_price'))
				$adv['discounted_price'] = $this->input->post('discounted_price');
				
			if($this->input->post('expired_date'))
				$adv['expired_date'] = mysql_datetime(strtotime($this->input->post('expired_date')));
			
			if($this->input->post('price_denote')){
				$adv['price_denote'] = $this->input->post('price_denote');
				}
			//check selected category whether 'Others' type or not
			//print_r($adv);
			$cond['id'] = $adv['categoryID'];
			//print_r($cond);
			$row = $this->Classifiedscatg_model->get_category_by_id($cond);
			
			if ($row[0]->cat_descriptions == 'Others'){
				$adv['features'] = $this->input->post('othcatg');
			}
				
			$adv['advertiserID']=$this->session->userdata('userid');
			
			if (isset($_FILES['userfile']['tmp_name']) && !empty($_FILES['userfile']['tmp_name'][0])) {
				$image_url = upload_files(250,200,'/classifieds');
				//is_array ($image_url)/
			
				if($image_url){
					$urls = array_values($image_url);
					log_message('INFO', 'inside image url array' . $urls[0]);
					$adv['primary_image'] = isset($urls[0]) ? $urls[0] : null;
					$adv['small_image1'] = isset($urls[1]) ? $urls[1] : null;
					$adv['small_image2'] = isset($urls[2]) ? $urls[2] : null;
					$adv['small_image3'] = isset($urls[3]) ? $urls[3] : null;
					
				}else{
					$data = array('message' => 'upload problem' . $image_url, 'action' => 'admin/profile/add');
					$data['user'] = null;
					$data['catg'] = $this->Classifiedscatg_model->get_classifieds_categories();
					$data['selectedCatg'] = $adv['categoryID'];
					$data['expired_date']  = $this->input->post('expired_date');
					$this->load->view('admin/classifieds/form', $data);
				}
			}
			$adv['posted_date'] = mysql_datetime();
			$this->load->model('Classified_model','',TRUE);
			
			$id = $this->Classified_model->save($adv);
			
			
			//set user  message
			$data['message'] = '<div class="success">Your advertisement has been successfully posted. Good Luck & Thanks for using MayKha as your partner. </div>';
		}
		
		//load view
		redirect('admin/classifieds/view', $data);
		$this->load->view('admin/classifieds/view', $data);
	}
}

?>
